<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

/*
Example usage:
	$messageStack = new CoreMessageStack();
	$messageStack->add('general', 'Error: Error 1', 'error');
	$messageStack->add('general', 'Error: Error 2', 'warning');
	if( $messageStack->size('general') > 0 ) {
		echo $messageStack->output('general');
	}
*/

class CoreMessageStack {
	var $messages;
	
	//constructor
	function CoreMessageStack() {
		$this->messages = array();
		
		if( isset($_SESSION['messageToStack']) ) {
			for($i=0, $n=sizeof($_SESSION['messageToStack']); $i<$n; $i++) {
				$this->add($_SESSION['messageToStack'][$i]['class'], $_SESSION['messageToStack'][$i]['text'], $_SESSION['messageToStack'][$i]['type']);
			}
			
			unset($_SESSION['messageToStack']);
		}
	}
	
	//methods
	function add($class, $message, $type = 'error') {
		if( $type == 'error' ) {
			$this->messages[] = array(
							'params' => 'class="messageStackError"',
							'class' => $class,
							'message' => $message,
							'text' => $this->_output_image('error.gif', LANGUAGES_MESSAGESTACK_ERROR) . '&nbsp;' . $message
								);
		} elseif( $type == 'warning' ) {
			$this->messages[] = array(
							'params' => 'class="messageStackWarning"',
							'class' => $class,
							'message' => $message,
							'text' => $this->_output_image('warning.gif', LANGUAGES_MESSAGESTACK_WARNING) . '&nbsp;' . $message
								);
		} elseif( $type == 'success' ) {
			$this->messages[] = array(
							'params' => 'class="messageStackSuccess"',
							'class' => $class,
							'message' => $message,
							'text' => $this->_output_image('success.gif', LANGUAGES_MESSAGESTACK_SUCCESS) . '&nbsp;' . $message
								);
		} else {
			$this->messages[] = array(
							'params' => 'class="messageStackError"',
							'class' => $class,
							'message' => $message,
							'text' => $message);
		}
	}
	
	function add_session($class, $message, $type = 'error' ) {
		if( !isset($_SESSION['messageToStack']) ) {
			$_SESSION['messageToStack'] = array();
		}
		
		$_SESSION['messageToStack'][] = array('class' => $class, 'text' => $message, 'type' => $type);
	}
	
	function reset() {
		$this->messages = array();
	}
	
	function output($class) {
		$output = array();
		
		for($i=0, $n=sizeof($this->messages); $i<$n; $i++ ) {
			if( $this->messages[$i]['class'] == $class ) {
				$output[] = $this->messages[$i];
			}
		}
		
		return $this->_output_format($output);
	}
	
	function size($class) {
		$count = 0;
		
		for($i=0, $n=sizeof($this->messages); $i<$n; $i++ ) {
			if( $this->messages[$i]['class'] == $class ) {
				$count++;
			}
		}
		
		return $count;
	}
	
	//private
	function _output_image($src, $title) {
		
		return $title;
	}
	
	function _output_format($output) {
		$output_string = '';
		
		$output_string .= '<div class="messagestack">';
		$output_string .= '<ul>';
		for($i=0, $n=sizeof($output); $i<$n; $i++) {
			$output_string .= '<li ' . $output[$i]['params'] . '>' . _lang($output[$i]['message']) . '</li>';
		}
		$output_string .= '</ul>';
		$output_string .= '</div>';
		
		return $output_string;
	}
}
//
?>